package stru1.day12;

/**
 * 112. 路径总和
 */
public class Solution2 {
    public static void main(String[] args) {
        System.out.println(hasPathSum(new TreeNode(1, new TreeNode(2), new TreeNode(3)), 5));
        System.out.println(hasPathSum(null, 0));
    }

    /**
     * 递归
     */
    public static boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) return false;
        if (root.left == null && root.right == null) return targetSum == root.val;
        return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
    }
}
